require("Cocos2d")
require("tools/DvString")

LayoutAlignType = {
  LAYOUT_ALIGN_LEFT = 0,
  LAYOUT_ALIGN_CENTER = 1,
  LAYOUT_ALIGN_RIGHT = 2,
  LAYOUT_ALIGN_TOP = 3,
  LAYOUT_ALIGN_BOTTOM = 4
}

local BaseLayout = class("BaseLayout", function(isWidget)
  if isWidget then
    return ccui.Widget:create()
  else
    return cc.NodeRGBA:create()
  end
end)

function BaseLayout:create(isWidget)
  local node = BaseLayout.new(isWidget)
  return node
end

function BaseLayout:layoutVertically(padding, align)
  align = align or LayoutAlignType.LAYOUT_ALIGN_CENTER
  local width = 0
  local height = padding * (self:getChildrenCount() - 1)
  local childs = self:getChildren()
  for v = 1, self:getChildrenCount() do
    local child = childs[v]
    if child:isVisible() then
      height = height + child:getContentSize().height * child:getScaleY()
      local w = child:getContentSize().width * child:getScaleX()
      if width < w then
        width = w
      end
    else
      height = height - padding
    end
  end
  self:setContentSize(cc.size(width, height))
  self:setAnchorPoint(0.5, 0.5)
  local poshei = 0
  for v = self:getChildrenCount(), 1, -1 do
    local child = childs[v]
    if child:isVisible() then
      local y = poshei + child:getContentSize().height * child:getScaleY() / 2
      child:setPositionY(y)
      poshei = poshei + child:getContentSize().height * child:getScaleY() + padding
      if align == LayoutAlignType.LAYOUT_ALIGN_LEFT then
        child:setPositionX(child:getContentSize().width * child:getScaleX() / 2)
      elseif align == LayoutAlignType.LAYOUT_ALIGN_RIGHT then
        child:setPositionX(width - child:getContentSize().width * child:getScaleX() / 2)
      else
        child:setPositionX(width / 2)
      end
    end
  end
end

function BaseLayout:layoutHorizontally(padding, align)
  align = align or LayoutAlignType.LAYOUT_ALIGN_CENTER
  local width = padding * (self:getChildrenCount() - 1)
  local height = 0
  local childs = self:getChildren()
  for v = 1, self:getChildrenCount() do
    local child = childs[v]
    if child:isVisible() then
      width = width + child:getContentSize().width * child:getScaleX()
      if height < child:getContentSize().height * child:getScaleY() then
        height = child:getContentSize().height
      end
    else
      width = width - padding
    end
  end
  self:setContentSize(cc.size(width, height))
  self:setAnchorPoint(0.5, 0.5)
  local posX = 0
  for v = 1, self:getChildrenCount() do
    local child = childs[v]
    if child:isVisible() then
      local x = posX + child:getContentSize().width * child:getScaleX() / 2
      child:setPositionX(x)
      posX = posX + (child:getContentSize().width * child:getScaleX() + padding)
      if align == LayoutAlignType.LAYOUT_ALIGN_TOP then
        child:setPositionY(height - child:getContentSize().height * child:getScaleX() / 2)
      elseif align == LayoutAlignType.LAYOUT_ALIGN_BOTTOM then
        child:setPositionY(child:getContentSize().height * child:getScaleX() / 2)
      else
        child:setPositionY(height / 2)
      end
    end
  end
end

function BaseLayout:layoutWithGrid(row, col, paddingX, paddingY)
  if self:getChildrenCount() <= 0 then
    return
  end
  local child = self:getChildren()[1]
  local w = child:getContentSize().width
  local h = child:getContentSize().height
  local width = w
  local height = 0
  local px = 0
  local py = 0
  local index = 0
  for v = 1, self:getChildrenCount() do
    local child = self:getChildren()[v]
    if child:isVisible() then
      child:setPositionX(px)
      child:setPositionY(py)
      child:setAnchorPoint(cc.p(0, 0))
      index = index + 1
      if index % col == 0 then
        if v < self:getChildrenCount() then
          width = px + w
          height = height + h + paddingY
          px = 0
          py = py - h - paddingY
        end
      else
        px = w + px + paddingX
      end
    end
  end
  local offy = height
  height = height + h
  for v = 1, self:getChildrenCount() do
    local child = self:getChildren()[v]
    child:setPositionY(child:getPositionY() + offy)
  end
  self:setContentSize(cc.size(width, height))
  self:setAnchorPoint(cc.p(0.5, 0.5))
end

function BaseLayout:layoutVerticallyWithCol(col, paddingX, paddingY)
  if self:getChildrenCount() <= 0 then
    return
  end
  local child = self:getChildren()[1]
  local w = child:getContentSize().width
  local h = child:getContentSize().height
  local width = w
  local height = 0
  local px = 0
  local py = 0
  local index = 0
  for v = 1, self:getChildrenCount() do
    local child = self:getChildren()[v]
    if child:isVisible() then
      child:setPositionX(px)
      child:setPositionY(py)
      child:setAnchorPoint(cc.p(0, 0))
      index = index + 1
      if index % col == 0 then
        if v < self:getChildrenCount() then
          width = px + w
          height = height + h + paddingY
          px = 0
          py = py - h - paddingY
        end
      else
        px = w + px + paddingX
      end
    end
  end
  local offy = height
  height = height + h
  for v = 1, self:getChildrenCount() do
    local child = self:getChildren()[v]
    child:setPositionY(child:getPositionY() + offy)
  end
  self:setContentSize(cc.size(width, height))
  self:setAnchorPoint(cc.p(0.5, 0.5))
end

return BaseLayout
